import {DateTimeFormatter, OffsetDateTime, ZoneId} from "@js-joda/core";
import {Locale} from "@js-joda/locale_zh";
import {eachAnyIfIsObject} from "./ObjectUtils";

export const YEAR_MONTH_DAY_FORMATER = DateTimeFormatter.ofPattern('YYYY年M月d日').withLocale(Locale.CHINESE)
export const YEAR_MONTH_DAY_INTERNATIONAL_FORMATER = DateTimeFormatter.ofPattern('YYYY-MM-dd').withLocale(Locale.CHINESE)
export const DATE_WITH_TIME_FORMATER = DateTimeFormatter.ofPattern('YYYY年M月d日 H时mm分ss秒').withLocale(Locale.CHINESE)
export const DATE_WITH_HOUR_MINUTE_FORMATER = DateTimeFormatter.ofPattern('YYYY年M月d日 H时mm分').withLocale(Locale.CHINESE)



export function preHandleObjectWithOffsetDateTimeParser(object:any){
    return eachAnyIfIsObject(object,(value, key, parent) => {
        if (typeof key==="string" && key.endsWith("DateTime")){
            parent[key] = OffsetDateTime.parse(value)
                .atZoneSameInstant(ZoneId.systemDefault())
                .toOffsetDateTime()
        }
    })
}